package com.applancer.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.applancer.model.FeedModel;
import com.applancer.model.GeneralModel;
import com.applancer.nativecmsflip.R;
import com.squareup.picasso.Picasso;

public class FeedAdapter extends ArrayAdapter<GeneralModel> {

	protected List<GeneralModel> ldata;

	public FeedAdapter(Context context, List<GeneralModel> objects) {
		super(context, R.layout.adapter_feed, objects);

		ldata = objects;
	}

	@Override
	public int getCount() {
		return ldata.size();
	}

	@Override
	public GeneralModel getItem(int position) {
		return ldata.get(position);
	}

	@Override
	public long getItemId(int position) {
		String str = ((FeedModel) ldata.get(position)).getId();
		return Integer.parseInt(str);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		FeedModel model = (FeedModel) getItem(position);

		ViewHolder holder;

		if (convertView == null || position == 0
				|| convertView.getTag() == null) {

			LayoutInflater inflater = LayoutInflater.from(parent.getContext());

			if (position == 0)
				convertView = inflater.inflate(R.layout.adapter_feed_big, null);
			else
				convertView = inflater.inflate(R.layout.adapter_feed, null);

			holder = new ViewHolder();
			holder.thumb = (ImageView) convertView.findViewById(R.id.thumb_id);
			holder.textview_title = (TextView) convertView
					.findViewById(R.id.title_id);
			holder.textview_author = (TextView) convertView
					.findViewById(R.id.author_id);
			holder.progressBar = (ProgressBar) convertView
					.findViewById(R.id.progressBar);
			// holder.textComment = (TextView) convertView
			// .findViewById(R.id.feed_comment_id);
			// holder.textLike = (TextView) convertView
			// .findViewById(R.id.feed_like_id);
			// holder.textShare = (TextView) convertView
			// .findViewById(R.id.feed_share_id);
			TextView cat_name = (TextView) convertView
					.findViewById(R.id.cat_name);
			if (cat_name != null)
				cat_name.setVisibility(View.GONE);

			if (position == 0)
				convertView.setTag(null);
			else
				convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		try {
			holder.textview_title.setText(model.getTitle());
		} catch (Exception e) {

		}

		if (model == null) {
			Log.e("Error", "model null");
			return convertView;
		}

		if (holder.textview_author == null) {
			Log.e("Error", "textview_author null");
			return convertView;
		}

		if (holder.progressBar != null)
			holder.progressBar.setVisibility(View.GONE);
		holder.textview_author.setText(model.getDate() == null ? "" : model
				.getDate());
		// holder.textComment
		// .setText(((model.getCountCommentFB().equals("")) ? "0" : model
		// .getCountCommentFB()) + " comment");
		// holder.textLike.setText(((model.getCountLikeFB().equals("")) ? "0"
		// : model.getCountLikeFB()) + " like");
		// holder.textShare.setText(((model.getCountShareFB().equals("")) ? "0"
		// : model.getCountShareFB()) + " share");

		Picasso.with(parent.getContext()).load(model.getUrlImageThumb())
				.placeholder(R.drawable.ic_launcher)
				.error(R.drawable.ic_launcher).fit().centerCrop()
				.into(holder.thumb);

		return convertView;
	}

	static class ViewHolder {
		ImageView thumb;
		TextView textview_title;
		TextView textview_author;
		ProgressBar progressBar;
		// TextView textComment;
		// TextView textLike;
		// TextView textShare;
	}

}
